<?php

class IndexController extends Lib2k_Controller_Action{


    public function preDispatch(){
        
   	}

    public function indexAction(){

        if ($this->getRequest()->isPost()) 
        {
            $params['searchtext'] = $this->getRequest()->getParam('searchtext'); 
            $params['cat'] = $this->getRequest()->getParam('cat');
            $redirectArray['controller'] = 'index';
            $redirectArray['action'] = 'index';
            if( $params['searchtext']){
                $redirectArray['searchtext'] = $params['searchtext'];    
            }
            
            $redirectArray['cat'] = $params['cat'];
            $this->_helper->Redirector->gotoRoute($redirectArray, 'default', true); 
           // $this->_helper->redirector('index', 'index', 'default', $params, true);
        }
        $filmModel = new Model_Film();
        $requestParams = $this->getRequest()->getParams();
     //   Zend_Debug::dump($requestParams);
        
        $page = (int) $this->getRequest()->getParam('page', 1);
        $countPerPage = 10;
        
        $cats = $filmModel->getCategories(28);
        
        $films = $filmModel->search($requestParams['searchtext'], $requestParams['cat'], $countPerPage, ($page - 1) * $countPerPage );
        $filmsCount = $filmModel->searchCount($requestParams['searchtext'], $requestParams['cat']);
        $filmsCount = $filmsCount[0]['count'];
      //  Zend_Debug::dump($filmsCount);
        
        foreach($films as &$film) {
            $film['categories'] = $filmModel->getCategories($film['id']);
        }        
        
        $searchForm = new Form_FilmSearchForm();
        $searchForm->setAction("/film/search/");
        $searchForm->populate($requestParams);
        
        $searchForm->isValid($requestParams);
        

        $paginator = Zend_Paginator::factory($filmsCount);
        $paginator->setItemCountPerPage($countPerPage);
        $paginator->setCurrentPageNumber($page);
        
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('p/paginator.phtml');
        $this->view->paginator = $paginator;          
        
        $this->view->films = $films;
        $this->view->filmsCount = $filmsCount;
        $this->view->searchForm = $searchForm;
       // $this->_redirect("/index/index/param");
       
       
            // new navigation menu item
        	$layout = $this->_helper->layout();
        	$view = $layout->getView();

        //	$config = new Zend_Config_Xml(APPLICATION_PATH . '/main_menu.xml');
            //Zend_Debug::dump($config["events"]['pages']);
        //	$navigation = new Zend_Navigation($config);
            
        	$navigation = $view->navigation();
            $a = $navigation->findOneBy("class", 'home');
            $a->setClass("active");
           // Zend_Debug::dump($a);            
            ///// end of initialing new menu item for event view
            //______________________________________________________      
             
    }
    
    public function parseAction(){
        $filmixParser = new Lib2k_Parsing_Filmix(); 
        $data = $filmixParser->parsePage(2);
        Zend_Debug::dump($data);
     // $s = file_get_contents('http://filmix.net');
   //   $contentText = iconv("Windows-1251", "UTF-8", $s);
     // echo $contentText;
  /**   $data = array('title' => 'title1',
                   'link' => 'filmix.net',
                   'rating' => '35',
                   'image' => 'link img',
                   'description' => 'description 132321',
                   'categories' => array(array('title' => 'cTitle1',
                                                'url' => 'cUrl'
                                                )
                                        )
                    
                    );
        $filmixModel = new Model_Film();
        $filmixModel->saveFilm('title', 'link2', 35, 'img', 'descr 123864sdf', array(array('title' => 'cat 1', 'link' => ' link1'),
                                                                                    array('title' => 'cat 2', 'link' => ' link2')
                                                                                    ));
        */
        $filmixModel = new Model_Film();
        foreach($data as $item) {
            $filmixModel->saveFilm($item['title'], $item['link'], $item['rating'], $item['image'], $item['description'], $item['categories']);
                      
        }
    }    

    public function paginatorAction(){
        $count = 95;
    $paginator = Zend_Paginator::factory($count);
    $paginator->setItemCountPerPage(10);
    $paginator->setCurrentPageNumber(1);
    
    Zend_View_Helper_PaginationControl::setDefaultViewPartial('p/paginator.phtml');
    $this->view->paginator = $paginator;        
    }
    
    public function testAction()
    {
     /*   $cUrl = new Lib2k_Parsing_cUrl();
        $imgPath = $cUrl->save_image('http://filmix.net/uploads/posts/2013-01/1357304919_udacha-naprokat.jpg', APPLICATION_PATH . "/../temp/");
        $imageModel = new Model_Image($imgPath);
        
        $cc = $imageModel->saveOrig();
        echo $cc;
        */
   /**     $dirMaxFileCount = 1;
        $dirPath = APPLICATION_PATH . "/../www/site/content/images/";
        $dirs = scandir($dirPath);
        var_dump($dirs);echo "<br/>";
        foreach($dirs as &$dir){ // get max int dirname
            $dir = intval($dir);
        };
        var_dump($dirs);
        $maxDirName = max($dirs);
        $maxDirPath = $dirPath . max($dirs);
        echo "<br />" . $maxDirName . "<br />";
        if(!file_exists($maxDirPath)){
            mkdir($maxDirPath);
        }
        $subDir = scandir($maxDirPath);
        if ((count($subDir) - 2) >= $dirMaxFileCount) {
            $returnMaxDirName = $maxDirName + 1;
            $returnMaxDirPath = $dirPath . $returnMaxDirName;echo $returnMaxDirName;
            mkdir($returnMaxDirPath);            
        } else {
            $returnMaxDirName = $maxDirName;
            $returnMaxDirPath = $dirPath . $maxDirName;            
        }
        echo "<br />" . $returnMaxDirPath; */
        $imageModel = new Model_Image(APPLICATION_PATH . "/../temp/1356685448_slepoe-pravosudie.jpe");
        $path = $imageModel->saveOrig();
        echo $path;
    }


}

